package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateLocationRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchLocationRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateLocationRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusLocationRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.ImportLocationResponse;
import com.wzdigit.wms.basic.client.response.basic.LocationApiResponse;
import com.wzdigit.wms.basic.client.response.basic.LocationResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.basic.LocationService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 库位 前端控制器
 * @author yangdongjie
 * @LocationController.java
 * @2020年12月31日下午5:56:18
 */
@RestController
@Api(tags = "库位")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class LocationController {

    @Autowired
    private LocationService locationService;

    private static final String REQUEST_MODEL = "/location";

	/**
	 * 库位（下拉框）列表（启用状态）
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "库位（下拉框）列表（启用状态）",notes = "库位（下拉框）列表（启用状态）")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/list")
    public List<LocationApiResponse> list(
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return locationService.list();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
	/**
	 * 根据库区查询库位列表（启用状态）
	 * @param zoneId
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "根据库区查询库位列表（启用状态）",notes = "根据库区查询库位列表（启用状态）")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getLocationByZone")
    public List<LocationApiResponse> getLocationByZone(
    		@ApiParam(value = "库区ID", required = true) 
    		@RequestParam Integer zoneId,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return locationService.getLocationByZone(zoneId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 查询库位列表
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @SuppressWarnings("rawtypes")
	@ApiOperation(value = "查询库位列表",notes = "查询库位列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchLocation")
    public PagerInfo<List<LocationResponse>> searchLocation(
    		SearchLocationRequest dto, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return locationService.searchLocation(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 查询库位
     * @param locId
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "查询库位",notes = "查询库位")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getLocation")
    public LocationResponse getLocation(
    		@ApiParam(value = "库位ID", required = true) 
    		@RequestParam Integer locId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return locationService.getLocation(locId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 创建库位
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "创建库位",notes = "创建库位")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createLocation")
    public LocationResponse createLocation(
    		@RequestBody @Valid CreateLocationRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return locationService.createLocation(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改库位
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改库位",notes = "修改库位")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateLocation")
    public LocationResponse updateLocation(
    		@RequestBody @Valid UpdateLocationRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return locationService.updateLocation(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改库位状态
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改库位状态",notes = "修改库位状态")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateStatus")
    public <T> Result<T> updateStatus(
    		@RequestBody @Valid UpdateStatusLocationRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return locationService.updateStatus(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 导出《库位基础信息》模板
     * @param servletRequest
     * @param servletResponse
     * @throws Exception 
     */
    @ApiOperation(value = "导出《库位基础信息》模板", notes = "导出《库位基础信息》模板")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/exportBasic")
    public void exportBasic(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
    		locationService.exportBasic(servletRequest, servletResponse);
        } catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
    
    /**
     * 导出《库位限制信息》模板
     * @param servletRequest
     * @param servletResponse
     * @throws Exception 
     */
    @ApiOperation(value = "导出《库位限制信息》模板", notes = "导出《库位限制信息》模板")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/exportLimit")
    public void exportLimit(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
    		locationService.exportLimit(servletRequest, servletResponse);
    	} catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
    
    /**
     * 导出《库位尺寸信息》模板
     * @param servletRequest
     * @param servletResponse
     * @throws Exception 
     */
    @ApiOperation(value = "导出《库位尺寸信息》模板", notes = "导出《库位基础信息》模板")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/exportSize")
    public void exportSize(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
    		locationService.exportSize(servletRequest, servletResponse);
    	} catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
    
    /**
     * 导出《库位坐标信息》模板
     * @param servletRequest
     * @param servletResponse
     * @throws Exception 
     */
    @ApiOperation(value = "导出《库位坐标信息》模板", notes = "导出《库位基础信息》模板")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/exportCoord")
    public void exportCoord(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
    		locationService.exportCoord(servletRequest, servletResponse);
    	} catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
    
    /**
     * 导入《库位基础信息》
     * @param file
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入《库位基础信息》", notes = "导出《库位基础信息》")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/importBasic")
    public ImportLocationResponse importBasic(MultipartFile file, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
			return locationService.importBasic(file, servletRequest, servletResponse);
    	} catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
    
    /**
     * 导入《库位限制信息》
     * @param file
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入《库位限制信息》", notes = "导出《库位限制信息》")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/importLimit")
    public ImportLocationResponse importLimit(MultipartFile file, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
			return locationService.importLimit(file, servletRequest, servletResponse);
    	} catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
    
    /**
     * 导入《库位尺寸信息》
     * @param file
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入《库位尺寸信息》", notes = "导出《库位尺寸信息》")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/importSize")
    public ImportLocationResponse importSize(MultipartFile file, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
			return locationService.importSize(file, servletRequest, servletResponse);
    	} catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
    
    /**
     * 导入《库位坐标信息》
     * @param file
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入《库位坐标信息》", notes = "导出《库位坐标信息》")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/importCoord")
    public ImportLocationResponse importCoord(MultipartFile file, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
			return locationService.importCoord(file, servletRequest, servletResponse);
    	} catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
}
